package org.example.springboot.controller;

import org.apache.commons.lang3.StringUtils;
import org.example.springboot.common.exception.ServerException;
import org.example.springboot.common.utils.Result;
import org.example.springboot.entity.User;
import org.example.springboot.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/auth")
public class AuthController {

    @Resource
    private UserService userService;

    // 用户登录
    @PostMapping("/login")
    public Result<Object> login(@RequestBody Map<String, Object> paramsMap) {
        String username = (String) paramsMap.get("username");
        String password = (String) paramsMap.get("password");
        try {
            if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) {
                return userService.login(username, password);
            }
            return Result.error("请检查参数是否正确!");
        } catch (Exception e) {
            throw new ServerException("登录失败", e);
        }
    }

    // 用户登出
    @PostMapping("/loginOut")
    public Result<Object> loginOut(@RequestBody Map<String, Object> paramsMap) {
        String username = (String) paramsMap.get("username");
        try {
            if (StringUtils.isNotBlank(username)) {
                return userService.loginOut(username);
            }
            return Result.error("请检查参数是否正确!");
        } catch (Exception e) {
            throw new ServerException("登出失败", e);
        }

    }

}
